public final class BinarySearch {
	public static int counter;

	/***************************************************************************
	 * Recusion is fun!
	 * 
	 * @param a
	 *            an sorted array, small to big
	 * @param key
	 * @param from
	 * @param to
	 * @return
	 */
	public static int search(int[] a, int key, int from, int to) {
		int middle = (from + to) / 2;
		counter++;
		if (to <= from)
			return -1;
		counter++;
		if (a[middle] == key)
			return middle;
		counter++;
		if (a[middle] < key)
			return search(a, key, middle, to-1);
		counter++;
		if (a[middle] > key)
			return search(a, key, from, middle);
		
		return -1;
	}
}
